// pages/doctor-authentication-1/doctor-authentication-1.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    conceal: !1,
    serve: !1,
    currentLevel: 1,
    level: 2,
    time: 1,
    avatarUrls: "",
    array: "",
    currentTab: 1,
    takes: false,
    workYears: '', // 医龄
    idNumber: '',
    jobArr: [],
    jobArrObj: [],
    index: '',
    ind:'',
    jobId:'',
    cityid:'',
    hospitalTypeArr:[],
    hospitalTypeObj:{},
    birth:'',      // 出生年份
    hospitalLevel:'',  // 医院等级
    errMsg: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      url: app.globalData.imgUrl,
      userInfo: wx.getStorageSync('userInfo')
    })
    this.getDoctor();
    this.dataSecondShow();
  },
  onShow() {
    const that = this;
    wx.getSetting({
      success(res) {

        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success(e) {

              // 允许console.log(e.errMsg:"authorize:ok")
              that.setData({
                errMsg: e.errMsg
              })

            },
            fail(e) {
              // 不允许console.log(e.errMsg:"authorize:fail auth deny")
              that.setData({
                errMsg: e.errMsg
              })
            }
          })
        }
      }
    })

  },

  // 数据二次回显
  dataSecondShow(){
    //console.log(wx.getStorageSync("userInformation"))
    // 基本信息
    if (wx.getStorageSync("userInformation")) {
      const userInformation = wx.getStorageSync("userInformation");
      this.setData({
        avatarUrls: userInformation.avatarUrls,
        name: userInformation.name,
        idNumber: userInformation.cardNo,
        hospital:userInformation.hospital,
        workYears:userInformation.dage,
        content:userInformation.content,
        hospitalLevel: userInformation.hospitalLevel,
        birth: userInformation.birth ,
        hospitalTypeName: userInformation.hospitalTypeName
      })
    }
    // 医生职位
    if(wx.getStorageSync("doctorJob")) {
      const job = wx.getStorageSync("doctorJob");
      this.setData({
        index: job.index,
        jobId: job.jobId
      })
    }
    // 擅长项目
    if (wx.getStorageSync("goodAtProjects")) {
      const data = wx.getStorageSync("goodAtProjects");
     
      this.setData({
        service: data.service,
        onename: data.onename
      })
    }
    // 所在城市
    if(wx.getStorageSync("doctorAddress")){
      const doctorAddress = wx.getStorageSync("doctorAddress");
      this.setData({
        provincename: doctorAddress.provincename,
        cityname: doctorAddress.cityname,
        districtname: doctorAddress.districtname,
        provinceid: doctorAddress.provinceid,
        cityid: doctorAddress.cityid,
        districtid: doctorAddress.districtid
      })
    }
    // 地理定位
    if (wx.getStorageSync("doctorLocation")) {
      const doctorLocation = wx.getStorageSync("doctorLocation");
      this.setData({
        address: doctorLocation.address,
        lng: doctorLocation.lng,
        lat: doctorLocation.lat
      })
    }
  },
 
  // 获取医生职务
  getDoctor() {
    const that = this;
    app.request({
      url: "doctor/doctor/getJobList",
      success: function(res) {
        let jobArr = [];
        for (let i = 0; i < res.data.length; i++) {
          jobArr.push(res.data[i].name)
        }
        that.setData({
          jobArr,
          jobArrObj: res.data
        })
      },
      fail: function(res) {}
    });
  },
  // 医生职务选择
  bindPickerChange: function(e) {
    const jobId = this.data.jobArrObj[e.detail.value].id;
    wx.setStorageSync("doctorJob", {
      index: e.detail.value,
      jobId
    })
    this.setData({
      index: e.detail.value,
      jobId
    })
  },
 
  closed: function() {
    this.setData({
      conceal: !1,
      takes: false,
    })
  },
  //下一步
  nextstep: function() {
    const { name, avatarUrls, hospital, provinceid, cityid, content, districtid, lng, lat, address, service, position, workYears, idNumber, jobId, hospitalLevel, birth, hospitalTypeName } = this.data;
    
    if (!app.validate(avatarUrls, 'required')) {
      app.showModal('请上传您的证件照人像');
      return;
    }
    if (!app.validate(name, 'required')) {
      app.showModal('请输入您的真实姓名');
      return;
    }
    if (!app.validate(hospitalLevel, 'required')) {
      app.showModal('请输选择医院类型');
      return;
    }
    if (!app.validate(birth, 'required')) {
      app.showModal('请输选择出生年份');
      return;
    }
    if (!app.validate(hospital, 'required')) {
      app.showModal('请输入您的所在医院');
      return;
    }
    if (!app.validate(jobId, 'required')) {
      app.showModal('请输入您的职务');
      return;
    }
    if (!app.validate(workYears, 'required')) {
      app.showModal('请输入您的从业时间');
      return;
    }
    if (!app.validate(idNumber, 'idNumber')) {
      app.showModal('请输入正确的身份证号码');
      return;
    }
    if (!app.validate(jobId, 'required')) {
      app.showModal('请输入职务');
      return;
    }
    if (!app.validate(service, 'required')) {
      app.showModal('请输入您的擅长项目');
      return;
    }
    if (!app.validate(provinceid, 'required')) {
      app.showModal('请选择省份');
      return;
    }
    if (!app.validate(cityid, 'required')) {
      app.showModal('请选择市');
      return;
    }
    if (!app.validate(districtid, 'required')) {
      app.showModal('请选择区县');
      return;
    }
    if (!app.validate(address, 'required')) {
      app.showModal('请选择位置信息');
      return;
    }
    if (!app.validate(content, 'required')) {
      app.showModal('请输入您的个人简介');
      return;
    }
    wx.navigateTo({
      url: "../doctor-authentication-2/doctor-authentication-2?name=" + name + "&avatarUrls=" + avatarUrls + "&hospital=" + hospital + "&provinceid=" + provinceid + "&cityid=" + cityid + "&content=" + content + "&districtid=" + districtid + "&lng=" + lng + "&lat=" + lat + "&address=" + address + "&service=" + service + "&position=" + jobId + "&dage=" + workYears + "&cardNo=" + idNumber + "&birth=" + birth + "&hospitalLevel=" + hospitalLevel + "&hospitalTypeName=" + hospitalTypeName
    })
  },
  getnameValue: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  gethospitalValue: function(e) {
    this.setData({
      hospital: e.detail.value
    })
  },
  getcontentValue: function(e) {
    this.setData({
      content: e.detail.value
    })
  },
  // getWorkYears(e) {
  //   this.setData({
  //     workYears: e.detail.value
  //   })
  // },
  bindDateChange: function (e) {
   
    this.setData({
      workYears: e.detail.value + "年"
    })
  },
  idNumber(e) {
    this.setData({
      idNumber: e.detail.value
    })
  },
  getpositionValue: function(e) {
    this.setData({
      position: e.detail.value
    })
  },
  bindBornYears(e){
    //console.log(e.detail.value)
    this.setData({
      birth: e.detail.value + '年'
    })
  },
 

  // 上传图片
  changeHead: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original"],
      sourceType: ["album", "camera"],
      success: function(e) {
        var a = e.tempFilePaths[0];
        app.uploadFile({
          url: 'doctor/member/uploadHead',
          filePath: a,
          success: function(res) {
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              avatarUrls: path
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
        that.setData({
          avatarUrl: a
        })
      }

    })
  },
  //获取医生位置信息lng/lat
  locationaddress: function() {
   
    var that = this;
    if (this.data.errMsg === "authorize:ok") {
      wx.chooseLocation({
        type: "wgs84",
        success: function (e) {
          that.setData({
            address: e.address,
            lng: e.longitude,
            lat: e.latitude,
          })
          wx.setStorageSync("doctorLocation", {
            address: e.address,
            lng: e.longitude,
            lat: e.latitude,
          })
        }
      })
    }
    if (this.data.errMsg === "authorize:fail auth deny") {
      wx.openSetting({
        success(res) {
          wx.authorize({
            scope: 'scope.userLocation',
            success(res) {  // 用户已经同意小程序获取用户信息
             
              wx.chooseLocation({
                type: "wgs84",
                success: function (e) {
                  that.setData({
                    address: e.address,
                    lng: e.longitude,
                    lat: e.latitude,
                  })
                  wx.setStorageSync("doctorLocation", {
                    address: e.address,
                    lng: e.longitude,
                    lat: e.latitude,
                  })
                }
              })
            }
          })
        }
      })
    }
   
    // wx.getSetting({
    //   success(res) {
    //     console.log(res)
    //     if (!res.authSetting['scope.userLocation']) {
    //       wx.authorize({
    //         scope: 'scope.userLocation',
    //         success() {
    //           wx.chooseLocation({
    //             type: "wgs84",
    //             success: function (e) {

    //               that.setData({
    //                 address: e.address,
    //                 lng: e.longitude,
    //                 lat: e.latitude,
    //               })
    //               wx.setStorageSync("doctorLocation", {
    //                 address: e.address,
    //                 lng: e.longitude,
    //                 lat: e.latitude,
    //               })
    //             }
    //           })
    //         }
    //       })
    //     } 
    //   }
    // })
  },
  //省
  updateaddress: function() {
    this.setData({
      'conceal': !0,
      'currentLevel': 1,
      'level': 2,
      'takes': true
    })
    // 获取省
    var that = this;
    app.request({
      url:'doctor/city/getCityList',
      success(res) {
        that.setData({
          provincelist: res.data,
        })
      }
    })
  },
  // 市和区县
  bindProvince: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.provinceid;
    var name = e.currentTarget.dataset.provincename;
    var level = that.data.level;
    var currentLevel = that.data.currentLevel;
    if (currentLevel == 1) {
      that.setData({
        provinceid: id,
        provincename: name,
      });
    } else if (currentLevel == 2) {
      that.setData({
        cityid: id,
        cityname: name,
      });
     
      this.hospitalType();
     
    } else if (currentLevel == 3) {
      that.setData({
        districtid: id,
        districtname: name,
      });
    }
    app.request({
      url:'doctor/city/getCityList',
      data: {
        id: id,
        level: level,
        currentLevel: currentLevel,
      },
      success(res) {
        that.setData({
          provincelist: res.data,
          level: level + 1,
          currentLevel: currentLevel + 1,
        })
      }
    })
    if (that.data.currentLevel == 3) {
      that.setData({
        'conceal': !1,
        'currentLevel': 1,
        'level': 2,
        'takes': false
      })
    }
  
    wx.setStorageSync("doctorAddress", {
      provincename: this.data.provincename,
      cityname: this.data.cityname,
      districtname: this.data.districtname,
      provinceid: this.data.provinceid,
      cityid: this.data.cityid,
      districtid: this.data.districtid
    })
  },


  // 医院类型
  hospitalType(){
    const that = this;

    app.request({
      url: "citycore/deviceapi/getRankList",
      success: function (res) {
        let hospitalTypeArr = [];
        for (let i = 0; i < res.data.list.length; i++) {
          hospitalTypeArr.push(res.data.list[i].name)
        }
        that.setData({
          hospitalTypeArr,
          hospitalTypeObj: res.data.list
        })
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });

  },
  hosType(e){
    let ind = e.detail.value;
    const hospitalTypeArr = this.data.hospitalTypeArr;
    const hospitalLevel = this.data.hospitalTypeObj[ind].id;
    this.setData({
      ind,
      hospitalLevel,
      hospitalTypeName: hospitalTypeArr[ind]
    })
  },

  // 擅长项目数据
  oneTypeServiceList: function(e) {
    var that = this;
    this.setData({
      'serve': !0,
      'time': 1,
      'takes': true
    })
    app.request({
      url: 'shopp/goodsTypeApi/getPage',
      data: { type: 2 },
      success: function(res) {
        if (!that.data.onetypelist) {
          that.setData({
            onetypelist: res.data.slice(1),
          });
        }
      }
    })
  },
  // 选择擅长项目
  clickTab: function (e) {
   
    var that = this;
    var array = that.data.onetypelist;
    for (var kk = 0; kk < array.length; kk++) {
      if (array[kk].id == e.target.dataset.id) {
        if (array[kk]["ids"] != 1) {
          array[kk]["ids"] = 1;
        } else {
          array[kk]["ids"] = 0;
        }
      }
    }
    that.setData({
      onetypelist: array,
    })
  },
  // 确定擅长项目
  countersign: function () {
    var that = this;
    var array = that.data.onetypelist;
    var array1 = [];
    var array2 = [];
    for (var i = 0; i < array.length; i++) {
      if (array[i]["ids"] == 1) {
        array1.push(array[i].id);
        array2.push(array[i].name);
      }
    }
    var array3 = array1.join(",");
    var array4 = array2.join(",");
    that.setData({
      serve: !1,
      service: array3,  // id
      onename: array4,  // name
      takes: false
    })
    wx.setStorageSync("goodAtProjects", {
      service: array3,  // id
      onename: array4,  // name
    })
  },

 
})